package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.mvp.contract.OrderDetailContract;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.HeDanListBean;
import com.tianhui.consignortwofei.mvp.model.enty.OrderCondition;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.presenter.OrderDetailPresenter;
import com.tianhui.consignortwofei.mvp.ui.adapter.HeDanListAdapter;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchDetailStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchStatusDingJin;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.FreightStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.HeDanDetailStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.InquirySourceStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.PaymentStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.SourceStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.SourceStatusDingJin;

import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/8/28
 * @description
 **/
public class HeDanDetailActivity extends BaseActivity<OrderDetailContract.View, OrderDetailContract.Presenter> implements OrderDetailContract.View {

    @BindView(R.id.activity_order_detail_recyclerView)
    YCRefreshView mRecyclerView;

    @BindView(R.id.activity_order_detail_actionImageView)
    ImageView mActionImageView;



    private HeDanListAdapter mAdapter;
    private OrderDetailPresenter mPresenter;
    private IOrderStatus mIOrderStatus;
    private OrderInfo mOrderInfo;
    private OrderModel mModel;
    private List<HeDanListBean> allList;
    private String firstBillno="";
    private String firstDispatchno="";

    @Override
    public int getLayoutId() {
        return R.layout.activity_hedan_detail;
    }

    @Override
    public OrderDetailContract.Presenter createPresenter() {
        mPresenter = new OrderDetailPresenter(this);
        return mPresenter;
    }

    @Override
    public OrderDetailContract.View createView() {
        return this;
    }


    @Override
    public void init() {

        setSubTitle("增加");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new HeDanListAdapter(getContext(), mIOrderStatus);
        mRecyclerView.setAdapter(mAdapter);

        mModel = new OrderModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (mOrderInfo != null) {
                firstBillno= mOrderInfo.billno;
                firstDispatchno= mOrderInfo.dispatchno;
                int orderType = bundle.getInt(KeyValue.KEY_TYPE);
                IOrderStatus iOrderStatus = createOrderStatus(orderType);
                if (iOrderStatus != null) {
                    requestOrderDetail();//获取数据

                } else {
                    showEmptyView();
                }
            } else {
                showEmptyView();
            }
        }

        allList = new ArrayList<>();

        initListener();
    }


    private void requestOrderDetail() {
        if (mOrderInfo != null && mIOrderStatus != null) {
            OrderCondition orderCondition = new OrderCondition();
            orderCondition.dispatchNo = mOrderInfo.dispatchno;
            orderCondition.billNo = mOrderInfo.billno;
            orderCondition.orderId = mOrderInfo.id;
            getHeDanList(orderCondition);
//            getPresenter().requestOrderDetail(mIOrderStatus.getOrderDetailObservable(orderCondition), true, mIOrderStatus.getOrderStatusType());
        }

    }

    private void initListener() {
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {


                switch (view.getId()) {
                    case R.id.tv_delete:
                        Object object = mAdapter.getAllData().get(position);
                        if (object instanceof HeDanListBean) {
                            HeDanListBean heDanListBean = (HeDanListBean) object;
                            if ("".equals(heDanListBean.getId())){
                                allList.remove(position);
                                showOrderDetailList(allList);
                            }else {
                                heDanDelete(heDanListBean,position);
                            }


                        }

                        break;
                }
            }
        });

//        mAdapter.setOnDetailOperateItemClickListener(new DetailAdapter.OnDetailOperateItemClickListener() {
//            @Override
//            public void onOperateClick(int position, OrderOperateInfo orderOperateInfo) {
//                if (orderOperateInfo != null) {
//                    IOrderOperate iOrderOperate = orderOperateInfo.iOrderOperate;
//                    if (iOrderOperate != null) {
//                        iOrderOperate.orderOperate(HeDanDetailActivity.this, orderOperateInfo);
//                    }
//                }
//            }
//        });

//        mActionImageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mIOrderStatus != null) {
//                    Bundle bundle = new Bundle();
//                    bundle.putInt(KeyValue.KEY_TYPE, mIOrderStatus.getOrderStatusType());
//                    bundle.putParcelable(KeyValue.KEY_ORDER_INFO, mOrderInfo);
//                    openActivity(OrderOperateActivity.class, bundle);
//                }
//            }
//        });
    }



    @Override
    public void onSubtitleClick() {
        HeDanListBean listBean=new HeDanListBean(firstBillno,"",firstDispatchno,"","");
        allList.add(listBean);
        showOrderDetailList(allList);
    }

    private IOrderStatus createOrderStatus(int type) {

        switch (type) {
            case OrderType.TYPE_SOURCE:
                mIOrderStatus = new SourceStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("货源单详情");
                return mIOrderStatus;
            case OrderType.TYPE_SOURCE_DINGJIN:
                mIOrderStatus = new SourceStatusDingJin();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("货源单详情");
                return mIOrderStatus;
            case OrderType.TYPE_INQUIRY:
                mIOrderStatus = new InquirySourceStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("询价单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH:
                mIOrderStatus = new DispatchStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("调度单详情");
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH_DINGJIN:
                mIOrderStatus = new DispatchStatusDingJin();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("调度单详情");
                return mIOrderStatus;

            case OrderType.TYPE_FREIGHT:
                mIOrderStatus = new FreightStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("运费计算");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_PAYMENT:
                mIOrderStatus = new PaymentStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("订单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH_DETAIL:
                mIOrderStatus = new DispatchDetailStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("订单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_HE_DAN:
                mIOrderStatus = new HeDanDetailStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("合单");
                showFloatActionButton(false);
                return mIOrderStatus;


        }
        return null;
    }

    private void showFloatActionButton(boolean isShow) {
        if (isShow) {
            mActionImageView.setVisibility(View.VISIBLE);
        } else {
            mActionImageView.setVisibility(View.GONE);
        }
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @Override
    public void setOrderInfo(OrderInfo orderInfo) {
        if (orderInfo != null)
            mOrderInfo = orderInfo;
    }

    @Override
    public void showOrderDetailList(List list) {
        if (list != null && list.size() > 0) {
            mAdapter.clear();
            mAdapter.addAll(list);
        } else {
            showEmptyView();
        }
    }

    @Override
    public void showEmptyView() {
        mRecyclerView.setEmptyView(R.layout.layout_empty_view);
        mRecyclerView.showEmpty();
    }

//    @Override
//    public void onEvent(CommonEvent event) {
//        switch (EventWhat.valueOf(event.getWhat())) {
//            case EVENT_REFRESH_ORDER_DETAIL:
//                requestOrderDetail();
//                break;
//            case EVENT_ACTIVITY_FINISH:
//                finish();
//                break;
//        }
//    }


    @OnClick(R.id.btn_hedan_true)
    public void hedanTrue() {
        sendHeDanTrue();
    }




    private void getHeDanList(OrderCondition orderCondition){
        Map<String, String> map = new HashMap<>();
//        map.put("limit", "10");
//        map.put("offset", "1");
//        map.put("order", "desc");
//        map.put("sort", "sendordertime");
//        map.put("page", orderCondition.page + "");
        map.put("dispatchno", orderCondition.dispatchNo);
        mModel.getHeDanList(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<HeDanListBean>>>() {
            @Override
            public void onNext(CommonResponse<List<HeDanListBean>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<HeDanListBean> list = commonResponse.getData();
//                    CommonDataListInfo data = commonResponse.getData();
//                        List<OrderInfo> list = data.records;
                        if (list != null && list.size() > 0) {
//                            firstBillno = list.get(0).getBillno();
//                            firstDispatchno = list.get(0).getDispatchno();
                            allList.clear();
                            allList.addAll(list);
//                            OrderInfo info = list.get(0);
//                            if (null != info && mIOrderStatus != null) {
//                                setOrderInfo(info);
                                showOrderDetailList(list);
//                            }
                        } else {
                            showEmptyView();
                        }


                } else {
                        showEmptyView();
                        setMsg(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                    setMsg(e.message);
                    showEmptyView();
            }
        });


    }


    private void sendHeDanTrue() {
        List<HeDanListBean> allData = mAdapter.getAllData();
//        Gson gson=new Gson();
//        String json = gson.toJson(allData);
//        Map<String, String> map = new HashMap<>();
//        map.put("name", json);

        mModel.sendHeDanTrue(this, allData, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    ToastUtil.showShortToast("提交成功");
                    finish();

                } else {
                    setMsg(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });
    }

    private void heDanDelete(HeDanListBean heDanListBean,int position) {
        Map<String, String> map = new HashMap<>();
//        map.put("limit", "10");
//        map.put("offset", "1");
//        map.put("order", "desc");
//        map.put("sort", "sendordertime");
//        map.put("page", orderCondition.page + "");
        map.put("id", heDanListBean.getId());
        mModel.heDanDelete(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<HeDanListBean>>>() {
            @Override
            public void onNext(CommonResponse<List<HeDanListBean>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    allList.remove(position);
                    showOrderDetailList(allList);

                } else {
                    setMsg(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });
    }


}
